//
//  YCActivitiesViewController.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/28.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  活动

#import "YCActivitiesViewController.h"
#import "YCActivitiesCell.h"
#import "YCActivitiesModel.h"

@interface YCActivitiesViewController ()<UITableViewDelegate,UITableViewDataSource>

/** tableView */
@property (nonatomic, strong) UITableView *tableView;

/** 测试数据  */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** 网络请求数据  */
@property (nonatomic, strong) NSArray *netDataArray;

@end

@implementation YCActivitiesViewController

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        
        YCActivitiesModel *model = [[YCActivitiesModel alloc] init];
        model.title = @"\"线上\"特价活动班正式来袭!";
        model.des = @"公安部\"新驾考\"4月1日实施务必记住几个重点";
        model.img = @"http://newsxml.cnool.net/newspic2011/2010/2010-12/2010-12-6/634272260653125000.jpg";
        model.addtime_str = @"一分钟前";
        model.source = @"凤凰网";
        model.url = @"http://www.baidu.com/";
        
        YCActivitiesModel *model1 = [[YCActivitiesModel alloc] init];
        model1.title = @"一每国际潜水执照培训一一泰国涛岛蔚蓝之旅";
        model1.des = @"公安部\"新驾考\"4月1日实施务必记住几个重点,大小家心啦";
        model1.img = @"http://www.sxol.com/Editor1/uploadfile/20110514093406867.jpg";
        model1.addtime_str = @"343";
        model1.source = @"中央台";
        model1.url = @"http://www.weibo.com/";
        
        YCActivitiesModel *model2 = [[YCActivitiesModel alloc] init];
        model2.title = @"公安部\"新驾考\"4月1日实施务必记住几个重点月1日实施务必记住几个重点";
        model2.img = @"http://www.zgqczj.com/file/upload/201308/20/11-24-26-64-581.jpg";
        model2.addtime_str = @"33";
        model2.source = @"汽车装具网";
        model2.url = @"http://www.boc.cn/";
        
        YCActivitiesModel *model3 = [[YCActivitiesModel alloc] init];
        model3.title = @"国家安全局表示:公安部\"新驾考\"4月1日实施务必记住几个重点";
        model3.img = @"http://www.rtsac.org/Upload/UEditor/Img/20150507/6356659266748775001808843.jpg";
        model3.addtime_str = @"5243";
        model3.source = @"一乘汽车网";
        model3.url = @"http://www.12306.cn/mormhweb/";
        
        YCActivitiesModel *model4 = [[YCActivitiesModel alloc] init];
        model4.title = @"国家安全局表示:公安部\"新驾考\"4月1日实施务必记住几个重点";
        model4.img = @"http://www.rtsac.org/Upload/UEditor/Img/20150507/6356659266748775001808843.jpg";
        model4.addtime_str = @"153";
        model4.source = @"一乘汽车网";
        model4.url = @"http://www.12306.cn/mormhweb/";
        
        [_dataArray addObjectsFromArray:@[model,model1,model2,model3,model4]];
        
    }
    return _dataArray;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.yc_width, self.view.yc_height-64) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([YCActivitiesCell class]) bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:@"activitesCell"];
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"活动";
    [self yc_addBackBarButtonItem];
    [self.view addSubview:self.tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    self.tableView.rowHeight = 263;// 251+间距12(上下各6)
    
    YCWeakSelf;
    self.tableView.mj_header = [YCRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    // 进入刷新状态
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 数据处理相关
- (void)loadNewData
{
    NSDictionary *params = @{//暂时不用传参
                             //                             @"uid":@"1",
                             //                             @"token":accessToken_Test,
                             };
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    [mgr POST:[YCAPIActivity appendBaseUrl] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.netDataArray = [YCActivitiesModel yc_objectArrayWithKeyValuesArray:responseObject];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        YCLog(@"%@",error.localizedDescription);
        [self.tableView.mj_header endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.netDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCActivitiesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"activitesCell"];
    cell.contentView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    cell.itemModel = self.netDataArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCLogFunc
}


#pragma mark - 点击事件



@end
